﻿using CPPEI.Coolzon.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Visifire.Gauges;

namespace CPPEI.Coolzon.UI.DataPartLib
{
    /// <summary>
    /// DataPartCircularGaugeV2.xaml 的交互逻辑
    /// </summary>
    public partial class DataPartCircularGaugeV2 : PartVisifireGauge
    {
        /// <summary>
        /// 数据加载动画
        /// </summary>
      

        Visifire.Gauges.NeedleIndicator _indicatorWCL;
        Visifire.Gauges.NeedleIndicator _indicatorGDFHL;
        Visifire.Gauges.CircularScale _circularScaleWCL;
        Visifire.Gauges.CircularScale _circularScaleGDFHL;




        [XProperty(XPropertyCategory.Other, "CircularGaugeV2Name", "仪表盘的名称")]
        public string XCircularGaugeV2Name
        {
            get
            {
                return (string)GetValue(CircularGaugeV2NameProperty);
            }
            set
            {
                SetValue(CircularGaugeV2NameProperty, value);
            }
        }

        [XProperty(XPropertyCategory.Other, "ForegroundImage", "仪表盘前景图片")]
        public ImageSource XForegroundImage
        {
            get
            {
                return (ImageSource)GetValue(ForegroundImageProperty);
            }
            set
            {
                SetValue(ForegroundImageProperty, value);
            }
        }

        [XProperty(XPropertyCategory.Other, "MinorDataRange", "次要仪表盘范围颜色集")]
        public List<DataRange> XMinorDataRange
        {
            get
            {
                return (List<DataRange>)GetValue(MinorDataRangeProperty);
            }
            set
            {
                SetValue(MinorDataRangeProperty, value);
            }
        }

        [XProperty(XPropertyCategory.Other, "MajorDataRange", "主要仪表盘范围颜色集")]
        public List<DataRange> XMajorDataRange
        {
            get
            {
                return (List<DataRange>)GetValue(MajorDataRangeProperty);
            }
            set
            {
                SetValue(MajorDataRangeProperty, value);
            }
        }


        /// <summary>
        /// Dependency property to Get/Set the CircularGaugeV2Name
        /// </summary>
        public static readonly DependencyProperty CircularGaugeV2NameProperty =
            DependencyProperty.Register("XCircularGaugeV2Name", typeof(string), typeof(DataPartCircularGaugeV2), new PropertyMetadata(new PropertyChangedCallback(OnValuePropertyChanged)));

        /// <summary>
        /// Dependency property to Get/Set the ForegroundImage
        /// </summary>
        public static readonly DependencyProperty ForegroundImageProperty =
            DependencyProperty.Register("XForegroundImage", typeof(ImageSource), typeof(DataPartCircularGaugeV2), new PropertyMetadata(new PropertyChangedCallback(OnValuePropertyChanged)));

        /// <summary>
        /// Dependency property to Get/Set the  MajorDataRange
        /// </summary>
        public static readonly DependencyProperty MajorDataRangeProperty =
            DependencyProperty.Register("XMajorDataRange", typeof(List<DataRange>), typeof(DataPartCircularGaugeV2), new PropertyMetadata(new PropertyChangedCallback(OnValuePropertyChanged)));

        /// <summary>
        /// Dependency property to Get/Set the  MinorDataRange
        /// </summary>
        public static readonly DependencyProperty MinorDataRangeProperty =
            DependencyProperty.Register("XMinorDataRange", typeof(List<DataRange>), typeof(DataPartCircularGaugeV2), new PropertyMetadata(new PropertyChangedCallback(OnValuePropertyChanged)));


        private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DataPartCircularGaugeV2 p = (DataPartCircularGaugeV2)d;
            p.RaisePropertyChanged(e.Property.Name);
        }

        public DataPartCircularGaugeV2()
        {
            InitializeComponent();
        }


        protected override void OnInit(PartContext context)
        {
            base.OnInit(context);

            ctlCircularGauge.Ranges.Clear();
            //设置默认模板
            ctlCircularGauge.DefaultTemplate = (DefaultTemplates)Enum.Parse(typeof(DefaultTemplates), "CircularGaugeBasic");
            ctlCircularGauge.Type = GaugeTypes.Circular;
            _circularScaleWCL = new Visifire.Gauges.CircularScale();
            _circularScaleWCL.BorderBrush = new SolidColorBrush(Colors.LawnGreen);
            _circularScaleWCL.BorderThickness = new Thickness(0);
            _circularScaleWCL.Name = "ScaleWCL";
            _circularScaleWCL.Minimum = 75;
            _circularScaleWCL.Maximum = 120;
            _circularScaleWCL.StartAngle = 160;
            _circularScaleWCL.SweepAngle = 105;
           
            ImageBrush foregroundImageBrush = new ImageBrush(XForegroundImage);
            _ctlBorderForeground.Background = foregroundImageBrush;

            ScaleLabels scaleLabels = new ScaleLabels();
            scaleLabels.Visibility = System.Windows.Visibility.Hidden;

            _circularScaleWCL.ScaleLabels = scaleLabels;

            Visifire.Gauges.MinorTicks minorTicks = new Visifire.Gauges.MinorTicks();
            minorTicks.Visibility = System.Windows.Visibility.Visible;

            //设置次要刻度指针的显示位置、宽度、长度
            minorTicks.Placement = (PlacementTypes)Enum.Parse(typeof(PlacementTypes), "Outside");
            minorTicks.Background = new SolidColorBrush(Colors.Red);

            _circularScaleWCL.MinorTicks = minorTicks;

            Visifire.Gauges.MajorTicks majorTicks = new Visifire.Gauges.MajorTicks();
            majorTicks.Visibility = System.Windows.Visibility.Visible;

            //设置主要要刻度指针的显示位置、宽度、长度
            majorTicks.Placement = (PlacementTypes)Enum.Parse(typeof(PlacementTypes), "Outside");
            majorTicks.Background = new SolidColorBrush(Colors.Red);

            _circularScaleWCL.MajorTicks = majorTicks;

            // 添加区域
            if (XMajorDataRange != null && XMajorDataRange.Count > 0)
            {
                foreach (DataRange dataRange in XMajorDataRange)
                {
                    Visifire.Gauges.Range range = new Visifire.Gauges.Range();
                    range.ScaleName = "ScaleWCL";
                    range.StartValue = dataRange.StartValue;
                    range.EndValue = dataRange.EndValue;
                    range.StartWidth = dataRange.StartWidth;
                    range.EndWidth = dataRange.EndWidth;
                    if (dataRange.Background != null)
                    {
                        range.Background = dataRange.Background.Brush;
                    }
                    if (dataRange.BorderBrush != null)
                    {
                        range.BorderBrush = dataRange.BorderBrush.Brush;
                    }
                    range.BorderThickness = dataRange.BorderThickness;
                    range.Placement = Visifire.Gauges.PlacementTypes.Center;
                    ctlCircularGauge.Ranges.Add(range);
                }
            }

            ctlCircularGauge.CircularScales.Add(_circularScaleWCL);

            _circularScaleGDFHL = new Visifire.Gauges.CircularScale();
            _circularScaleGDFHL.BorderBrush = new SolidColorBrush(Colors.LawnGreen);
            _circularScaleGDFHL.BorderThickness = new Thickness(0);
            _circularScaleGDFHL.Name = "ScaleGDFHL";
            _circularScaleGDFHL.StartAngle = 275;
            _circularScaleGDFHL.Minimum = 75;
            _circularScaleGDFHL.Maximum = 120;
            _circularScaleGDFHL.SweepAngle = 105;
          
            ScaleLabels GDFHLscaleLabels = new ScaleLabels();
            GDFHLscaleLabels.Visibility = System.Windows.Visibility.Hidden;

            _circularScaleGDFHL.ScaleLabels = GDFHLscaleLabels;

            Visifire.Gauges.MinorTicks GDFHLminorTicks = new Visifire.Gauges.MinorTicks();
            GDFHLminorTicks.Visibility = System.Windows.Visibility.Visible;

            //设置次要刻度指针的显示位置、宽度、长度
            GDFHLminorTicks.Placement = (PlacementTypes)Enum.Parse(typeof(PlacementTypes), "Outside");
            GDFHLminorTicks.Background = new SolidColorBrush(Colors.Red);

            _circularScaleGDFHL.MinorTicks = GDFHLminorTicks;

            Visifire.Gauges.MajorTicks DGFHLmajorTicks = new Visifire.Gauges.MajorTicks();
            DGFHLmajorTicks.Visibility = System.Windows.Visibility.Visible;

            //设置主要要刻度指针的显示位置、宽度、长度
            DGFHLmajorTicks.Placement = (PlacementTypes)Enum.Parse(typeof(PlacementTypes), "Outside");
            DGFHLmajorTicks.Background = new SolidColorBrush(Colors.Red);

            _circularScaleGDFHL.MajorTicks = DGFHLmajorTicks;

            // 添加区域
            if (XMinorDataRange != null && XMinorDataRange.Count > 0)
            {
                foreach (DataRange dataRange in XMinorDataRange)
                {
                    Visifire.Gauges.Range range = new Visifire.Gauges.Range();
                    range.ScaleName = "ScaleGDFHL";
                    range.StartValue = dataRange.StartValue;
                    range.EndValue = dataRange.EndValue;
                    range.StartWidth = dataRange.StartWidth;
                    range.EndWidth = dataRange.EndWidth;
                    if (dataRange.Background != null)
                    {
                        range.Background = dataRange.Background.Brush;
                    }
                    if (dataRange.BorderBrush != null)
                    {
                        range.BorderBrush = dataRange.BorderBrush.Brush;
                    }
                    range.BorderThickness = dataRange.BorderThickness;
                    range.Placement = Visifire.Gauges.PlacementTypes.Center;
                    ctlCircularGauge.Ranges.Add(range);
                }
            }


            ctlCircularGauge.CircularScales.Add(_circularScaleGDFHL);

        }

        protected override Visifire.Gauges.Gauge GetVisifireGauge()
        {
            return ctlCircularGauge;
        }

        /// <summary>
        /// 组件从非激活转到激活
        /// </summary>
        protected override void OnActive(PartContext context)
        {
            ctlCircularGauge.Indicators.Clear();
            base.OnActive(context);
        }


        protected override void OnDataBinding(ServiceProxy.DataServiceRef.DataResult dataResult)
        {
            if (dataResult.DataSeries.Length > 0 && dataResult.DataSeries[0].DataItems.Length > 0)
            {
                if (ctlCircularGauge.Indicators.Any())
                {
                    ctlCircularGauge.Indicators.Clear();
                }
                if (_indicatorWCL == null)
                {
                    _indicatorWCL = new Visifire.Gauges.NeedleIndicator();

                    _indicatorWCL.ScaleName = "ScaleWCL";
                    //指针背景颜色
                    _indicatorWCL.Background = new SolidColorBrush(Colors.Red);
                    ctlCircularGauge.Indicators.Add(_indicatorWCL);
                }
                else
                {
                    _indicatorWCL.ScaleName = "ScaleWCL";
                    //指针背景颜色
                    _indicatorWCL.Background = new SolidColorBrush(Colors.Red);
                    ctlCircularGauge.Indicators.Add(_indicatorWCL);
                }
                _indicatorWCL.Placement = PlacementTypes.Outside;
                _indicatorWCL.IndicatorWidth = 0.3;

                if (_indicatorGDFHL == null)
                {
                    _indicatorGDFHL = new Visifire.Gauges.NeedleIndicator();

                    _indicatorGDFHL.ScaleName = "ScaleGDFHL";
                    //指针背景颜色
                    _indicatorGDFHL.Background = new SolidColorBrush(Colors.Red);

                    ctlCircularGauge.Indicators.Add(_indicatorGDFHL);
                }
                else
                {
                    _indicatorGDFHL.ScaleName = "ScaleGDFHL";
                    //指针背景颜色
                    _indicatorGDFHL.Background = new SolidColorBrush(Colors.Red);

                    ctlCircularGauge.Indicators.Add(_indicatorGDFHL);
                }

                _indicatorGDFHL.Placement = PlacementTypes.Outside;
                _indicatorGDFHL.IndicatorWidth = 0.3;

                List<CPPEI.Coolzon.ServiceProxy.DataServiceRef.DataSeries> dataSeriesList = dataResult.DataSeries.ToList();


                if (dataSeriesList != null)
                {
                    ctlCircularGauge.Indicators[0].Value = 0;
                    ctlCircularGauge.Indicators[0].BeginAnimation(NeedleIndicator.ValueProperty, new DoubleAnimation(0, Convertor.ToDouble(dataResult.DataSeries[0].DataItems[0].Value), new Duration(TimeSpan.FromSeconds(XDataAnimationDuration))));

                    ctlCircularGauge.Indicators[1].Value = 0;
                    ctlCircularGauge.Indicators[1].BeginAnimation(NeedleIndicator.ValueProperty, new DoubleAnimation(0, Convertor.ToDouble(dataResult.DataSeries[1].DataItems[0].Value), new Duration(TimeSpan.FromSeconds(XDataAnimationDuration))));

                }

            }
        }
    }
}
